/**
 * 添加或者修改页面
 */
var CbAdvertisementInfoDlg = {
    data: {
        id: "",
        advertisement: "",
        images: "",
        type: "",
        createTime: "",
        createUser: "",
        updateTime: "",
        updateUser: "",
        iscontact: ""
    }
};

layui.use(['form', 'admin', 'ax','laydate','upload','formSelects'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var upload = layui.upload;















    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/cbAdvertisement/addItem", function (data) {
            Feng.success("添加成功！");
            window.location.href = Feng.ctxPath + '/cbAdvertisement'
        }, function (data) {
            Feng.error("添加失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });

    $('#cancel').click(function(){
        window.location.href = Feng.ctxPath + '/cbAdvertisement'
    });

    //普通图片上传
    upload.render({
        elem: '#imagesUpload'
        ,url: Feng.ctxPath + '/api/oss/upload_file' //改成您自己的上传接口
        ,before: function(obj){
            console.log("upload img obj: "+ obj);
            // 预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#filePreview').attr('src', result); //图片链接（base64）
            });
        }
        ,done: function(res){
            console.log("upload img data: "+ res.data);
            var fileId = res.data;
            // $("#images").val(fileId);
            var ajax = new $ax(Feng.ctxPath + "/system/updateAvatar", function (data) {
                Feng.success(res.message);
                $("#images").val(fileId);
            }, function (data) {
                Feng.error("修改失败!" + data.responseJSON.message + "!");
            });
            ajax.set("fileId", res.data);
            ajax.start();
        }
        ,error: function(){
            Feng.error("上传文件失败！");
        }
    });
});